import {generatorDynamicRouter} from '@/utils/routerUtil'
import {initNavigation,appKey,activeKey} from "./initNavigation"
const appSetting = {
    namespaced: true,
    state: {
        //所有的菜单
        menus: [],
        //有权限的按钮
        buttons: {},
        //所有的按钮
        allButtons: {},
        //当前应用KEY ，这里分为前端key 和后端选中的应用key。
        appKey: structuredClone(appKey),

        menuStyle: 'inline',

        //头部主题色
        background: '#1A5EFF',
        //左侧主题色
        sideBar_Background: '#29304c',
        //系统风格
        systemStyle: 'around',
        menuPath: [],
        menuMap: [],
        //左边树选中的key
        selectedKeys: ['home-index'],
        loading: false,
        //左边树展开的组；
        openKeys: [],
        //当前用户对象
        user: {},
        logined: false,
        //导航栏 默认首页;
        navigation: structuredClone(initNavigation),
        //导航栏当 前选中相 ;
        activeKey: structuredClone(activeKey),

        appmenuId: '',
        //列表按钮窗口类型 default默认 会缓存列表 ； window弹窗，newPage新页面；
        showType: 'window',
        //是否打开的是微前端 true/false
        openFront: false,
        //加载微前端时的loading
        openFrontLoading: false,
        //用户是否首次登录
        isFirstLogin: false,
        //首页是否折叠左边菜单，
        collapsed: false,
        //选中（激活）的微前端数据
        activeFront:{},
        //密码模式 (default 默认模式，md5 md5加密模式)
        passwordmode: 'default',
        //当前网站名称
        siteName:"",
        //隐藏头部与左侧区域
        hideArea:false,
        //所有应用
        allApps:{},
        appManager:{},
        // 前端类型 (user,前台,admin 后台)
        frontType:'admin',
        //应用模式
        appMode:'single'
    },
    mutations: {
        /**
         * 设计应用管理员
         *  1、平台级别：{default：true/false}
         *  2、应用级别：{appId:true/false}
         * @param state
         * @param collapsed
         */
        setAppManager: (state, appManager) => {
            state.appManager = {} ;
            state.appManager = appManager ;
        },
        setCollapsed: (state, collapsed) => {
            state.collapsed = collapsed ;
        },
        /**
         * 设置menuid
         * @param state
         * @param appmenuId
         */
        setsystemStyle: (state, systemStyle) => {
            state.systemStyle = systemStyle
        },
        setAppMenuId: (state, appmenuId) => {
            state.appmenuId = appmenuId;
        },

        //配置选择的 tab 数组 ；
        setNavigation: (state, navigation) => {
            state.navigation = navigation;
        },
        clearNavigation: (state) => {
            state.navigation =  structuredClone(initNavigation);
        },
        setActiveKey: (state, activeKey) => {//配置tab选中key
            let obj=state.activeKey;
         //   obj[state.frontType]=activeKey;
            obj['admin']=activeKey;
        },
        setOpenKeys: (state, openKeys) => {
            state.openKeys = openKeys;
        },
        setMenus: (state, menus) => {
            state.menus = menus;
        },
        setMenuMap: (state, menuMap) => {
            state.menuMap = menuMap;
        },
        setMenuStyle: (state, menuStyle) => {
            state.menuStyle = menuStyle;
        },

        setMenuPath: (state, menuPath) => {
            state.menuPath = menuPath
        },
        setButtons: (state, buttons) => {
            state.buttons = buttons;
        },
        setAllButtons: (state, allButtons) => {
            state.allButtons = allButtons;
        },
        /**
         * 这里有两个应用key
         * @param state
         * @param appKey
         */
        setAppKey: (state, appKey) => {
            let obj=state.appKey;
            obj[state.frontType]=appKey;
        },
        /**
         * 清理appKey
         * @param state
         */
        clearAppKey: (state) => {
            state.appKey=structuredClone(appKey);
        },
        /**
         * 清理 activeKey
         * @param state
         */
        clearActiveKey: (state) => {
            state.activeKey=structuredClone(activeKey);
        },
        setSelecteKeys: (state, defaultKeys) => {//配置左边菜单选中key
            state.selectedKeys = defaultKeys;
        },
        setloading: (state, load) => {
            state.loading = load
        },
        setUser: (state, user) => {
            state.user = user
        },
        setLogined: (state, logined) => {
            state.logined = logined
        },
        backgroundkey: (state, background) => {
            state.background = background
        },
        sideBar_Backgroundkey: (state, sideBar_Background) => {
            state.sideBar_Background = sideBar_Background
        },
        setShowTypeActive: (state, type) => {
            state.showType = type
        },
        setIsFirstLogin: (state, isFirstLogin) => {
            state.isFirstLogin = isFirstLogin
        },
        setOpenFront: (state, type) => {// 是否打开的是微前端 true/false
            state.openFront = type;
        },
        setOpenFrontLoading: (state, type) => {//打开微前端时的loading
            state.openFrontLoading = type;
        },
        setActiveFront:(state, data) => {//设置选中（激活）的微前端的数据
            state.activeFront = data;
        },
        /**
         * 设置密码模式
         * @param state
         * @param passwordmode
         */
        setPasswordmode: (state, passwordmode) => {
            state.passwordmode = passwordmode
        },
        setSiteName: (state, siteName) => {
            state.siteName = siteName;
        },
        setHideArea: (state, hideArea) => {
            state.hideArea = hideArea;
        },
        setAllApps: (state, allApps) => {
            state.allApps = allApps;
        },
        /**
         * 设置前端类型。
         * @param state
         * @param frontType
         */
        setFrontType: (state, frontType) => {
            state.frontType = 'admin';
        },
        /**
         * 应用模式。
         * @param state
         * @param appMode
         */
        setAppMode: (state, appMode) => {
            state.appMode = appMode;
        },
    },
    actions: {
        /**
         * 构建获取和构建路由和用户与菜单信息
         * @param commit
         * @param data
         * @returns {Promise<any>}
         * @constructor
         */
        GenerateRoutes({commit}) {
            return new Promise(resolve => {
                generatorDynamicRouter().then(res => {
                    var routers = res.routers;
                    var menus = res.menus;
                    var menuMap = res.menuMap;
                    var buttons = res.buttons;
                    var appMode = res.appMode;

                    // debugger;

                    var allButtons = res.allButtons;
                    var user = res.user;
                    commit('setMenuMap', menuMap);
                    commit('setMenus', menus);
                    commit('setButtons', buttons);
                    commit('setAllButtons', allButtons);
                    commit('setUser', user);
                    commit('setAppMode', appMode);
                    window.localStorage.setItem('curUser',user.userId)
                    commit('setLogined', true);
                    //设置首次登录状态
                    let firstLogin=user.isFirstLogin == "YES";
                    commit('setIsFirstLogin',firstLogin);


                    resolve(routers)
                })
            })
        },

    },
    getters: {
        /**
         * 获取选中的appKey
         * @param state
         * @returns {*}
         */
        getAppKey: state => {
            let appKey=state.appKey[state.frontType];
            return appKey;
        },
        /**
         * 获取选中的菜单。
         * @param state
         * @returns {*}
         */
        getActiveKey: state => {
            let activeKey=state.activeKey[state.frontType];
            return activeKey;
        },
    }
}
export default appSetting;


